﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using PMIS.BLL;
using PMIS.BLL.Department;

namespace PMIS.UI.Department
{
	/// <summary>
	/// 部门班组选择
	/// </summary>
	public partial class 部门班组选择 : BaseForm
	{
		#region 私有变量
		/// <summary>
		/// 部门信息列表
		/// </summary>
		private List<部门> listDepartment = null;
		#endregion

		#region 单例
		/// <summary>
		/// 本类的实例
		/// </summary>
		private static 部门班组选择 _form = null;
		/// <summary>
		/// 用于本类实现单例的Locker
		/// </summary>
		private static object locker = new object();
		/// <summary>
		/// 构造函数
		/// </summary>
		private 部门班组选择()
		{
			InitializeComponent();
		}
		/// <summary>
		/// 获取本窗体的实例
		/// </summary>
		/// <returns></returns>
		public static 部门班组选择 GetInstance()
		{
			if (_form == null || _form.IsDisposed)
			{
				lock (locker)
				{
					if (_form == null || _form.IsDisposed)
						_form = new 部门班组选择();
				}
			}

			return _form;
		}
		#endregion

		#region 公共事件
		/// <summary>
		/// 单位选择事件
		/// </summary>
		public static event EventHandler<NotifyEventArgs<部门>> SelectionChanged = null;
		#endregion

		#region 窗体事件
		/// <summary>
		/// 窗体载入事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void 部门班组选择_Load(object sender, EventArgs e)
		{
			//	刷新部门班组树
			new Thread(new ThreadStart(UpdateFormView)).Start();
		}
		#endregion

		#region 界面显示
		/// <summary>
		/// 更新界面显示
		/// </summary>
		private void UpdateFormView()
		{
			try
			{
				if (tvDepartment.InvokeRequired)
				{
					tvDepartment.Invoke(new Action(UpdateFormView));
				}
				else
				{
					//	获取所有部门班组信息
					listDepartment = Singleton<BL_部门>.GetInstance().GetAll();
					if (listDepartment == null)
						throw new Exception("获取部门信息时发生错误!");

					tvDepartment.BeginUpdate();
					tvDepartment.Nodes.Clear();
					//	递推一次性建树
					UpdateTreeNodesByParent(null);
					tvDepartment.EndUpdate();
				}
			}
			catch (Exception ex)
			{
				ShowExceptionMessage(ex);
			}
		}
		/// <summary>
		/// 根据父节点来递推建树
		/// </summary>
		/// <param name="tnParent">父节点</param>
		private void UpdateTreeNodesByParent(TreeNode tnParent)
		{
			if (listDepartment == null)
				return;

			List<部门> list = tnParent == null ?
				listDepartment.FindAll(t => t.上级部门ID == "0") :
				listDepartment.FindAll(t => t.上级部门ID == (tnParent.Tag as 部门).ID);
			if (list == null)
				return;

			#region 增加部门节点
			List<部门> departments = list.FindAll(t => t.类型 == "部门");
			departments.Sort((x, y) => x.名称.CompareTo(y.名称));
			foreach (部门 department in departments)
			{
				TreeNode node = new TreeNode();
				node.Text = department.名称;
				node.Tag = department;
				node.SelectedImageIndex = node.ImageIndex = 0;

				//	递推建树
				UpdateTreeNodesByParent(node);

				if (tnParent == null)
				{
					tvDepartment.Nodes.Add(node);
					node.Expand();
				}
				else
				{
					tnParent.Nodes.Add(node);
				}
			}
			#endregion

			#region 增加班组节点
			List<部门> teams = list.FindAll(t => t.类型 == "班组");
			teams.Sort((x, y) => x.名称.CompareTo(y.名称));
			foreach (部门 team in teams)
			{
				TreeNode node = new TreeNode();
				node.Text = team.名称;
				node.Tag = team;
				node.SelectedImageIndex = node.ImageIndex = 1;

				if (tnParent == null)
				{
					tvDepartment.Nodes.Add(node);
				}
				else
				{
					tnParent.Nodes.Add(node);
				}
			}
			#endregion
		}
		/// <summary>
		/// 查找并更新节点状态
		/// </summary>
		/// <param name="tnParent"></param>
		/// <param name="name"></param>
		public void UpdateTreeNodeStateByDepartmentAndTeamName(TreeNode tnParent, string name)
		{
			if (string.IsNullOrEmpty(name))
				return;

			TreeNodeCollection tnc = tnParent == null ? tvDepartment.Nodes : tnParent.Nodes;
			bool fit = false, contains = false;
			foreach (TreeNode node in tnc)
			{
				fit = false;
				//	收起节点
				node.Collapse();

				//	按名称查询
				fit = node.Text.Contains(name);

				//	递推查找
				UpdateTreeNodeStateByDepartmentAndTeamName(node, name);

				//	符合查询条件则节点文本颜色变为醒目的红色,不符合查询条件的节点文本颜色变为原来的黑色
				node.ForeColor = fit ? Color.Red : Color.Black;
				//	是否需要逐级展开父节点
				contains = contains | fit;
			}

			if (contains)
			{
				//	逐级展开父节点
				TreeNode pn = tnParent;
				while (pn != null)
				{
					pn.Expand();
					pn = pn.Parent;
				}
			}
		}
		#endregion

		#region 触发事件
		/// <summary>
		/// 查询
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btQuery_Click(object sender, EventArgs e)
		{
			if (string.IsNullOrEmpty(tb名称.Text.Trim()))
			{
				ShowMessageBox("请输入要查询的部门或班组名称!");
				return;
			}

			try
			{
				tvDepartment.BeginUpdate();
				//	查找并显示
				UpdateTreeNodeStateByDepartmentAndTeamName(null, tb名称.Text.Trim());
				tvDepartment.EndUpdate();
			}
			catch (Exception ex)
			{
				ShowExceptionMessage(ex);
			}
		}
		/// <summary>
		/// 选择
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btSelect_Click(object sender, EventArgs e)
		{
			if (tvDepartment.SelectedNode == null)
			{
				ShowErrorMessage("请选择部门或班组节点!");
				return;
			}

			try
			{
				部门 item = tvDepartment.SelectedNode.Tag as 部门;
				if (item == null)
					return;

				//	调用通知事件
				if (SelectionChanged != null)
					SelectionChanged(this, new NotifyEventArgs<部门>(item));

				DialogResult = DialogResult.OK;
				this.Close();
			}
			catch (Exception ex)
			{
				ShowExceptionMessage(ex);
			}
		}
		/// <summary>
		/// 退出
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btExit_Click(object sender, EventArgs e)
		{
			DialogResult = DialogResult.Cancel;
			this.Close();
		}
		#endregion
	}
}